﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


namespace ShopInTheEvent
{
    public partial class Form1 : Form
    {
        private DataHelper dh;
        private List<ProductInfo> productList;

        public Form1()
        {
            InitializeComponent();
            dh = new DataHelper();
            
        }

        private void bt_ShowAll_Click(object sender, EventArgs e)
        {
            lv_ProductInfo.Items.Clear();
            
            productList = dh.GetAllProductInfo();

           
            
            foreach (ProductInfo product in productList) 
            {
                var listViewItem = new ListViewItem(product.StringInfo());
                lv_ProductInfo.Items.Add(listViewItem);
            }

            ItemsStatus.Text = Convert.ToString(lv_ProductInfo.Items.Count);
        }

        private void bt_Search_Click(object sender, EventArgs e)
        {
            
            lv_ProductInfo.Items.Clear();
            List<ProductInfo> productList;
            productList = dh.FindTheProduct(tb_ProductForeCode.Text);



            foreach (ProductInfo product in productList)
            {
                var listViewItem = new ListViewItem(product.StringInfo());
                lv_ProductInfo.Items.Add(listViewItem);
            }

            ItemsStatus.Text = Convert.ToString(lv_ProductInfo.Items.Count);
        }

        private void bt_AddNewProduct_Click(object sender, EventArgs e)
        {
            AddNewProduct addnew = new AddNewProduct();
            addnew.Show();
        }

        private void lv_ProductInfo_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right) 
            {
                if (lv_ProductInfo.FocusedItem.Bounds.Contains(e.Location) == true) 
                {
                    cMS_RightClickMenu.Show(Cursor.Position);
                }
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string value = "I want to add...";
                if (InputBox(lv_ProductInfo.SelectedItems[0].SubItems[1].Text, "quantity:", ref value) == DialogResult.OK)
                {

                    int nrAdded = dh.UpdateStock(lv_ProductInfo.SelectedItems[0].SubItems[0].Text, Convert.ToInt32(value));
                    if (nrAdded > 0)
                    {
                        MessageBox.Show("Succesfully added to the database!");
                    }
                    else
                    {
                        MessageBox.Show("Error while adding products!");
                    }
                    bt_ShowAll.PerformClick();

                }
            }catch(Exception)
            {
                MessageBox.Show("Somethings wrong, please check your input value!");
            }
        }

        public static DialogResult InputBox(string title, string promptText, ref string value)
        {
            Form form = new Form();
            Label label = new Label();
            TextBox textBox = new TextBox();
            Button buttonOk = new Button();
            Button buttonCancel = new Button();

            form.Text = title;
            label.Text = promptText;
            textBox.Text = value;

            buttonOk.Text = "OK";
            buttonCancel.Text = "Cancel";
            buttonOk.DialogResult = DialogResult.OK;
            buttonCancel.DialogResult = DialogResult.Cancel;

            label.SetBounds(9, 20, 372, 13);
            textBox.SetBounds(12, 36, 372, 20);
            buttonOk.SetBounds(228, 72, 75, 23);
            buttonCancel.SetBounds(309, 72, 75, 23);

            label.AutoSize = true;
            textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
            buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
            buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

            form.ClientSize = new Size(396, 107);
            form.Controls.AddRange(new Control[] { label, textBox, buttonOk, buttonCancel });
            form.ClientSize = new Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
            form.FormBorderStyle = FormBorderStyle.FixedDialog;
            form.StartPosition = FormStartPosition.CenterScreen;
            form.MinimizeBox = false;
            form.MaximizeBox = false;
            form.AcceptButton = buttonOk;
            form.CancelButton = buttonCancel;

            DialogResult dialogResult = form.ShowDialog();
            value = textBox.Text;
            return dialogResult;
        }

        private void removeToolStripMenuItem1_Click(object sender, EventArgs e)
        {

            if (MessageBox.Show("Are you sure?", "Sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                int nrRemoved = dh.DeleteTheProduct(lv_ProductInfo.SelectedItems[0].SubItems[0].Text);
                if (nrRemoved > 0)
                {
                    MessageBox.Show("Succesfully removed from the database!");
                }
                else
                {
                    MessageBox.Show("Error while removing product!");
                }
                bt_ShowAll.PerformClick(); 
            }

        }

        private void changePriceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                string value = "I want to change price to...";
                if (InputBox(lv_ProductInfo.SelectedItems[0].SubItems[1].Text, "New Price:", ref value) == DialogResult.OK)
                {

                    int nrChanged = dh.ChangePrice(lv_ProductInfo.SelectedItems[0].SubItems[0].Text, Convert.ToDouble(value));
                    if (nrChanged > 0)
                    {
                        MessageBox.Show("Succesfully changed in the database!");
                    }
                    else
                    {
                        MessageBox.Show("Error while changing price!");
                    }
                    bt_ShowAll.PerformClick();

                }
            }catch(Exception)
            {
                MessageBox.Show("Somethings wrong, please check your input value!");
            }
        }



    }
}
